package com.abaaba.project2.service.wx.wxserviceimpl;

import com.abaaba.project2.bean.*;
import com.abaaba.project2.bean.System;
import com.abaaba.project2.bean.vo.wx.WXFloorGoodsListBeanVO;
import com.abaaba.project2.bean.vo.wx.WXHomeIndexGoodsAndAllVO;
import com.abaaba.project2.mapper.*;
import com.abaaba.project2.util.DateToStringUtil;
import com.abaaba.project2.util.StringToDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.abaaba.project2.service.wx.WXHomeIndexService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WXHomeIndexServiceImpl implements WXHomeIndexService {

    /*newGoodsList*/
    @Autowired
    GoodsMapper goodsMapper;

    //"channel"
    @Autowired
    CategoryMapper categoryMapper;

    //"couponList"
    @Autowired
    CouponMapper couponMapper;
    //"grouponList" 这个是个goods表
    /*@Autowired
    GoodsMapper goodsMapper;*/

    //"banner"
    @Autowired
    AdMapper adMapper;

    //"brandList"
    @Autowired
    BrandMapper brandMapper;

    //"hotGoodsList"  这也是一个goods
    /*@Autowired
    BrandMapper brandMapper;*/
    @Autowired
    AdTopicMapper adTopicMapper;



    @Override
    public WXHomeIndexGoodsAndAllVO.DataBean wxHomeIndexSearchAllDataBaseTable() {

        /*--------------------------------先拼newGoodsList（上）--------------------------------*/
        /*这里的new Goods就是说最新时间范围内的六个数据*/
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        //criteria.andBrandIdEqualTo();
        //List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);

        //是否是新品
        /*这里就直接查询时间，根据时间查询前六个数据，第一个为最新加入的数据,只有6个*/
        List<Goods> newGoodsDateList = goodsMapper.selectByNewDateList();
        List<WXHomeIndexGoodsAndAllVO.DataBean.NewGoodsListBean> newGoodsListBeans = new ArrayList<>();

        for (Goods goods : newGoodsDateList) {
            WXHomeIndexGoodsAndAllVO.DataBean.NewGoodsListBean newGoodsListBean = new WXHomeIndexGoodsAndAllVO.DataBean.NewGoodsListBean(goods.getId(),goods.getName(),goods.getBrief(),goods.getPicUrl(),goods.getIsNew(),goods.getIsHot(),goods.getCounterPrice(),goods.getRetailPrice());
            newGoodsListBeans.add(newGoodsListBean);
        }
        /*newGoodsListBeans 为第一个数据*/

        /*--------------------------------先拼newGoodsList（下）--------------------------------*/

        /*--------------------------------拼couponList（上）--------------------------------*/
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria1 = couponExample.createCriteria();

        List<Coupon> coupons = couponMapper.selectByExampleOrderBy();
        ArrayList<WXHomeIndexGoodsAndAllVO.DataBean.CouponListBean> couponListBeans = new ArrayList<>();
        //过期优惠券不显示
        int i = 0;
        for (Coupon coupon : coupons) {
            DateToStringUtil dateToStringUtil = new DateToStringUtil();
            String CouponAddTime =null;
            String CouponUpdateTime = null;
            if (coupon.getStartTime()!=null && coupon.getUpdateTime()!=null){
            CouponAddTime = dateToStringUtil.dateToString(coupon.getStartTime());
            CouponUpdateTime = dateToStringUtil.dateToString(coupon.getUpdateTime());
            }
            /*time_type如果是0就显示days,如果是*/
            i++;
            WXHomeIndexGoodsAndAllVO.DataBean.CouponListBean couponListBean =null;
            if (coupon.getTimeType() == 0){
                couponListBean = new WXHomeIndexGoodsAndAllVO.DataBean.CouponListBean(coupon.getId(),coupon.getName(),coupon.getDesc(),coupon.getTag(),coupon.getDiscount(),coupon.getMin(),coupon.getDays(),null,null);
            }else if (coupon.getTimeType() == 1){
                couponListBean = new WXHomeIndexGoodsAndAllVO.DataBean.CouponListBean(coupon.getId(),coupon.getName(),coupon.getDesc(),coupon.getTag(),coupon.getDiscount(),coupon.getMin(),coupon.getDays(),CouponAddTime,CouponUpdateTime);
            }
            Date date = new Date();

            couponListBeans.add(couponListBean);

            if (i == 3){
                break;
            }
        }
        /*couponListBean 为第二个数据*/
        /*--------------------------------拼couponList（下）--------------------------------*/

        /*--------------------------------拼channel（上）--------------------------------*/
        /*只显示前五个*/
        String str = "L1";
        Integer limitNumber=5;
        List<Category> categoryList = categoryMapper.selectByChannerLimitFive(str,limitNumber);
        ArrayList<WXHomeIndexGoodsAndAllVO.DataBean.ChannelBean> channelBeans = new ArrayList<>();
        for (Category category : categoryList) {
            WXHomeIndexGoodsAndAllVO.DataBean.ChannelBean channelBean = new WXHomeIndexGoodsAndAllVO.DataBean.ChannelBean(category.getId(),category.getName(),category.getIconUrl());
            channelBeans.add(channelBean);
         }
        /*channelBeans即为所求*/
        /*--------------------------------拼channel（下）--------------------------------*/

        //"grouponList"团购  说是不做
        /*--------------------------------拼channel（下）--------------------------------*/
        new WXHomeIndexGoodsAndAllVO.DataBean.GrouponListBean();
        ArrayList<WXHomeIndexGoodsAndAllVO.DataBean.GrouponListBean> grouponListBeans = new ArrayList<>();
        /*--------------------------------拼channel（下）--------------------------------*/

        //"banner" 对应Ad
        /*--------------------------------拼banner（下）--------------------------------*/
        AdExample adExample = new AdExample();
        AdExample.Criteria criteriaBanner = adExample.createCriteria();
        criteriaBanner.andEnabledEqualTo(true);
        List<Ad> adList = adMapper.selectByExample(adExample);
        List<WXHomeIndexGoodsAndAllVO.DataBean.BannerBean> bannerBeans = new ArrayList<>();
        for (Ad ad : adList) {
            WXHomeIndexGoodsAndAllVO.DataBean.BannerBean bannerBean = new WXHomeIndexGoodsAndAllVO.DataBean.BannerBean(ad.getId(),ad.getName(),ad.getLink(),ad.getUrl(),ad.getPosition(),ad.getContent(),ad.getEnabled(),ad.getAddTime(),ad.getUpdateTime(),ad.getDeleted());
            bannerBeans.add(bannerBean);
        }
        //bannerBeans
        /*--------------------------------拼banner（下）--------------------------------*/

        //brandList->>BrandMapper
        /*--------------------------------拼brandList（上）--------------------------------*/
        List<Brand> brandList = brandMapper.selectByBrandToShowList();
        ArrayList<WXHomeIndexGoodsAndAllVO.DataBean.BrandListBean> brandListBeans = new ArrayList<>();
        for (Brand brand : brandList) {
            WXHomeIndexGoodsAndAllVO.DataBean.BrandListBean brandListBean = new WXHomeIndexGoodsAndAllVO.DataBean.BrandListBean(brand.getId(),brand.getName(),brand.getDesc(),brand.getPicUrl(),brand.getFloorPrice());
            brandListBeans.add(brandListBean);
        }
        //brandListBeans
        /*--------------------------------拼brandList（下）--------------------------------*/

        //"hotGoodsList"
        /*--------------------------------拼hotGoodsList（下）--------------------------------*/
        /*不知道限定几个，跟公网一样6个*/
        Integer hotGoodsLimitNumber = 6;
        List<Goods> goodsList = goodsMapper.selectByHotGoodsShowList(hotGoodsLimitNumber);
        ArrayList<WXHomeIndexGoodsAndAllVO.DataBean.HotGoodsListBean> hotGoodsListBeans = new ArrayList<>();
        for (Goods goods : goodsList) {
            WXHomeIndexGoodsAndAllVO.DataBean.HotGoodsListBean hotGoodsListBean = new WXHomeIndexGoodsAndAllVO.DataBean.HotGoodsListBean(goods.getId(),goods.getName(),goods.getBrief(),goods.getPicUrl(),goods.getIsNew(),goods.getIsHot(),goods.getCounterPrice(),goods.getRetailPrice());
            hotGoodsListBeans.add(hotGoodsListBean);
        }
        //hotGoodsListBeans
        /*--------------------------------拼hotGoodsList（下）--------------------------------*/

        //topicList ->>AdTopicMapper
        /*--------------------------------拼hotGoodsList（下）--------------------------------*/
        AdTopicExample adTopicExample = new AdTopicExample();
        AdTopicExample.Criteria criteriaAdTopic = adTopicExample.createCriteria();

        List<AdTopic> adTopicList = adTopicMapper.selectByExample(adTopicExample);
        ArrayList<WXHomeIndexGoodsAndAllVO.DataBean.TopicListBean> topicListBeans = new ArrayList<>();
        for (AdTopic adTopic : adTopicList) {
            WXHomeIndexGoodsAndAllVO.DataBean.TopicListBean topicListBean = new WXHomeIndexGoodsAndAllVO.DataBean.TopicListBean(adTopic.getId(),adTopic.getTitle(),adTopic.getSubtitle(),adTopic.getPrice(),adTopic.getReadCount(),adTopic.getPicUrl());
            topicListBeans.add(topicListBean);
        }
        //topicListBeans
        /*--------------------------------拼hotGoodsList（下）--------------------------------*/

        //floorGoodsList
        /*--------------------------------floorGoodsList（下）--------------------------------*/
        /*可以复用这几个方法*/
        String str2 = "L1";
        Integer limitNumber2=4;
        //List<Category> categoryList2 = categoryMapper.selectByChannerLimitFive(str2,limitNumber2);
        List<Category> categoryList2 = categoryMapper.selectByChannerLimitFive2(str2,limitNumber2);

        WXHomeIndexGoodsAndAllVO.DataBean.FloorGoodsListBean floorGoodsListBean = new WXHomeIndexGoodsAndAllVO.DataBean.FloorGoodsListBean();
        /*根据categoryid 找到 pid List，根据PID 再去找goods表中的数据*/

        ArrayList<WXHomeIndexGoodsAndAllVO.DataBean.FloorGoodsListBean> floorGoodsListBeans = new ArrayList<>();
        for (Category categoryPid : categoryList2) {
            List<Category> categoryPidList = categoryMapper.selectByPidOfId(categoryPid.getId());
            /*找到PID之后，再找到goods数据*/
            for (Category category : categoryPidList) {
                criteria.andCategoryIdEqualTo(category.getId());
                //List<Goods> goodsList1 = goodsMapper.selectByExample(goodsExample);
                List<Goods> goodsList1 = goodsMapper.selectByExampleOfGoods(category.getId());
                ArrayList<WXFloorGoodsListBeanVO> wxFloorGoodsListBeanVOS = new ArrayList<>();
                if (goodsList1.size() != 0){
                int flag = 0;
                for (Goods goods : goodsList1) {
                    WXFloorGoodsListBeanVO wxFloorGoodsListBeanVO = new WXFloorGoodsListBeanVO(goods.getId(),goods.getName(),goods.getBrief(),goods.getPicUrl(),goods.getIsNew(),goods.getIsHot(),goods.getCounterPrice(),goods.getRetailPrice());
                   if (flag<6) {
                       wxFloorGoodsListBeanVOS.add(wxFloorGoodsListBeanVO);
                       flag++;
                   }
                }
                }
                WXHomeIndexGoodsAndAllVO.DataBean.FloorGoodsListBean floorGoodsListBean2 = new WXHomeIndexGoodsAndAllVO.DataBean.FloorGoodsListBean(category.getName(),category.getId(),wxFloorGoodsListBeanVOS);
                floorGoodsListBeans.add(floorGoodsListBean2);
            }
        }


       /* for (Category category : categoryList2) {
            criteria.andCategoryIdEqualTo(category.getId());
            //List<Goods> goodsList1 = goodsMapper.selectByExample(goodsExample);
            List<Goods> goodsList1 = goodsMapper.selectByExampleOfGoods(category.getId());
            ArrayList<WXFloorGoodsListBeanVO> wxFloorGoodsListBeanVOS = new ArrayList<>();
            for (Goods goods : goodsList1) {
                WXFloorGoodsListBeanVO wxFloorGoodsListBeanVO = new WXFloorGoodsListBeanVO(goods.getId(),goods.getName(),goods.getBrief(),goods.getPicUrl(),goods.getIsNew(),goods.getIsHot(),goods.getCounterPrice(),goods.getRetailPrice());
                wxFloorGoodsListBeanVOS.add(wxFloorGoodsListBeanVO);
            }
            WXHomeIndexGoodsAndAllVO.DataBean.FloorGoodsListBean floorGoodsListBean2 = new WXHomeIndexGoodsAndAllVO.DataBean.FloorGoodsListBean(category.getName(),category.getId(),wxFloorGoodsListBeanVOS);
            floorGoodsListBeans.add(floorGoodsListBean2);
        }*/
        /*然后再根据categoryList2，来查询数据库goods*/

        /*--------------------------------floorGoodsList（下）--------------------------------*/

        /*团购没有写*/
        /*--------------------------------总拼接--------------------------------*/
        WXHomeIndexGoodsAndAllVO.DataBean dataBean = new WXHomeIndexGoodsAndAllVO.DataBean(newGoodsListBeans,couponListBeans,channelBeans,grouponListBeans,bannerBeans,brandListBeans,hotGoodsListBeans,topicListBeans,floorGoodsListBeans);

        return dataBean;
    }
}
